package com.efast.dmom.mgmt.controller.les;

import com.efast.cafe.framework.bean.ResultBean;
import com.efast.dmom.mgmt.entity.les.TwDocOrderDetails;
import com.efast.dmom.mgmt.entity.les.TwDocOrderHeader;
import com.efast.dmom.mgmt.service.les.TwDocOrderHeaderService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import java.util.ArrayList;
import java.util.List;

/**
 * 出库订单
 * 
 * 设计到的表：
 * a)	出库订单表TW_DOC_ORDER_HEADER、TW_DOC_ORDER_DETAILS
 * b)	分配表 TW_ACT_ALLOC_LOT_D
 * c)	拣货任务表 TW_OUTB_TASK_HEADER、TW_OUTB_TASK_DETAIL
 * d)	库存表TW_INV_SKU_LOC_T_L
 * @author chlp
 */
@Controller
@RequestMapping(value="/twDocOrderHeader")
@Api(value = "/twDocOrderHeader", description = "物流执行管理-->出库-->出库订单")
public class TwDocOrderHeaderController {
	
	@Autowired
	private TwDocOrderHeaderService twDocOrderHeaderService;
	
	/**
	 * 【保存】
	 * 出库订单header的新增   后台需要生成：SO编号/SO状态/创建人/创建人类型/创建时间
	 * @param twDocAsnHeader
	 * @return
	 */
	@ApiOperation(value = "/update",notes = "【保存】")
	@RequestMapping(value = "/update" , method=RequestMethod.POST)
	public @ResponseBody ResultBean update(@RequestBody TwDocOrderHeader twDocOrderHeader){
		ResultBean result=new ResultBean(true,"'");
		twDocOrderHeaderService.save(twDocOrderHeader);
		result.setMessage("保存成功");
		return result;
	}
	
	/**
	 * 【新增】
	 * 出库订单header的新增   后台需要生成：SO编号/SO状态/创建人/创建人类型/创建时间
	 * @param twDocAsnHeader
	 * @return
	 */
	@RequestMapping(value = "/save" , method=RequestMethod.POST)
	public @ResponseBody ResultBean save(@RequestBody TwDocOrderHeader twDocOrderHeader){
		ResultBean result=new ResultBean(true,"'");
		twDocOrderHeaderService.insertTwDocOrderHeader(twDocOrderHeader);
		result.setMessage("新增成功");
		return result;
	}
	
	
	/**
	 * 【按单关闭】
	 * g)点击“按单关闭”，关闭整单
	 * @param twDocAsnHeader
	 * @return
	 */
	@ApiOperation(value = "按单关闭", httpMethod = "POST", notes = "按单关闭")
	@RequestMapping(value = "/close" , method=RequestMethod.POST)
	public @ResponseBody ResultBean close(@RequestBody TwDocOrderHeader twDocOrderHeader){
		ResultBean result=new ResultBean(true,"'");
		twDocOrderHeaderService.updateNonEmptyTwDocOrderHeaderById(twDocOrderHeader);
		result.setMessage("按单关闭成功");
		return result;
	}
	
	
	/**
	 * 【删除】
	 * f)点击“删除”，可以删除出库订单数据，可以删除明细，也可以整单删除
	 * @param twDocAsnHeader
	 * @return
	 */
	@RequestMapping(value = "/delete" , method=RequestMethod.POST)
	public @ResponseBody ResultBean delete(@RequestBody TwDocOrderHeader twDocOrderHeader){
		ResultBean result=new ResultBean(true,"'");
		twDocOrderHeaderService.deleteTwDocOrderHeaderById(twDocOrderHeader.getTwDocOrderHeaderId());
		result.setMessage("删除成功");
		return result;
	}
	
	
	/**
	 * 【按单分配】
	 * i)点击“按单分配”，实现整单分配
	 * @param twDocAsnHeader
	 * @return
	 */
	@RequestMapping(value = "/distributionByBill" , method=RequestMethod.POST)
	public @ResponseBody ResultBean distributionByBill(@RequestBody TwDocOrderHeader twDocOrderHeader){
		ResultBean result=new ResultBean(true,"'");
		twDocOrderHeaderService.distributionByBill(twDocOrderHeader);
		result.setMessage("按单分配成功");
		return result;
	}
	
	
	/**
	 * 【按单生成拣货任务】
	 * k)点击“按单生成拣货任务”，分配完成后整单产生拣货任务
	 * @param twDocAsnHeader
	 * @return
	 */
	@RequestMapping(value = "/generatePickingByBill" , method=RequestMethod.POST)
	public @ResponseBody ResultBean generatePickingByBill(@RequestBody TwDocOrderHeader twDocOrderHeader){
		ResultBean result=new ResultBean(true,"'");
		twDocOrderHeaderService.generatePickingByBill(twDocOrderHeader);
		result.setMessage("按单生成拣货任务成功");
		return result;
	}
	
	
	/**
	 * 【取消分配】
	 * m)点击“取消分配”，整单取消分配，只有拣货确认之前可以取消分配
	 * @param twDocAsnHeader
	 * @return
	 */
	@RequestMapping(value = "/cancelDistribution" , method=RequestMethod.POST)
	public @ResponseBody ResultBean cancelDistribution(@RequestBody TwDocOrderHeader twDocOrderHeader){
		ResultBean result=new ResultBean(true,"'");
		twDocOrderHeaderService.cancelDistribution(twDocOrderHeader);
		result.setMessage("取消分配成功");
		return result;
	}
	
	/**
	 * 【订单取消】
	 * @param twDocAsnHeader
	 * @return
	 */
	@RequestMapping(value = "/cancel" , method=RequestMethod.POST)
	public @ResponseBody ResultBean cancel(@RequestBody TwDocOrderHeader twDocOrderHeader){
		ResultBean result=new ResultBean(true,"'");
		twDocOrderHeaderService.cancel(twDocOrderHeader);
		result.setMessage("订单取消成功");
		return result;
	}
	
	/**
	 * 【确认发运】--Shipment
	 * @param twDocAsnHeader
	 * @return
	 */
	@RequestMapping(value = "/shipment" , method=RequestMethod.POST)
	public @ResponseBody ResultBean shipment(@RequestBody TwDocOrderHeader twDocOrderHeader){
		ResultBean result=new ResultBean(true,"'");
		List<TwDocOrderDetails> list = new ArrayList<>();
		twDocOrderHeaderService.shipment(twDocOrderHeader,list);
		result.setMessage("发运成功");
		return result;
	}
	
	
}
